
/*
	api查询回调解析结果格式一览

    | deliverState(投递状态) | routes(路径) 


*/


// ---------------- url 拼装 ---------------- //

// module.exports.expressUrlForShunFeng = function(expressNumber) {
// 	return ("http://www.sf-express.com/sf-service-web/service/bills/" + expressNumber + "/routes?app=bill&lang=sc&region=cn&translate=");
// };

// module.exports.expressUrlForShenTong = function(expressNumber) {
// 	return ("http://120.24.171.130:8080/StoAppPro/QueryOrderList?mailNo=" + expressNumber);
// };

// module.exports.expressUrlForZhongTong = function() {
// 	return "http://api.zto.cn/json.aspx";
// };

module.exports.expressUrlForUniversal = function(expressCompany, expressNumber) {
	return ("https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?appid=4001&com=" + expressCompany + "&nu=" + expressNumber);
};

// ---------------- result 解析 --------------- //

// module.exports.expressParseForShunFeng = function(result) {
	
// 	if (result.length() == 0) {
// 		return "";
// 	}

// 	var jsonObject = JSON.parse(result);

// };

module.exports.expressParseForZhongTong = function(result) {
	
};

module.exports.expressParseForShenTong = function(result) {

};

module.exports.expressParseForUniversal = function(result) {
	console.log(result);
	if (result.length === 0) {
		return undefined;
	}

	var resultDictionary = {};
	var jsonObject = JSON.parse(result);

	if (jsonObject.status == "-3") {// 没查询到
		resultDictionary.deliverState = -1;
	} else if (jsonObject.status == "0") {// 正常
			
		var routes = jsonObject.data.info.context;
		var state = 1;// 揽件

		// 解析物流状态，百度返回的state不准确
		if (routes.length > 0) {
			for (var i = 0; i < routes.length; i++) {
				var aRoute = routes[i].desc;
				console.log('[Debug] check ' + aRoute);
				if (aRoute.indexOf('已签收') != -1) {
					state = 3;
					break;
				} else if (aRoute.indexOf('疑难件') != -1) {
					state = 2;
					break;
				} else if (aRoute.indexOf('派件中') != -1) {
					state = 5;
					break;
				} else if (aRoute.indexOf('发往') != -1 || aRoute.indexOf('发出') != -1) {
					state = 0;
					break;
				}
			}
		}

		console.log("[Debug] parse state is : " + state);
		resultDictionary.routes = routes;
		resultDictionary.deliverState = state;
	}

	return resultDictionary;
};

